package com.cn.xact.ty_treasured_book.activity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.TYApplicationState;
import com.cn.xact.ty_treasured_book.R.layout;
import com.cn.xact.ty_treasured_book.R.menu;
import com.cn.xact.ty_treasured_book.activity.HomeActivity.MyNetworkCallBack;
import com.cn.xact.ty_treasured_book.config.TYConfigBase;
import com.cn.xact.ty_treasured_book.net.NetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkImpl;
import com.cn.xact.ty_treasured_book.ui.TextedProgressbar;
import com.cn.xact.ty_treasured_book.util.UIHelper;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class PhoneGuideAppForMailActivity extends Activity {

    private TextedProgressbar mDownloadProgress = null;
    private NetworkImpl mDownloadNetwork;
    private String mAppRoot = TYConfigBase.TY_LOCAL_DIR + TYConfigBase.TY_LOCAL_APP_INFO_DIR + TYConfigBase.TY_LOCAL_APP_INFO_DIR_MAIL_CALENDAR;
    private String mPicPath = "preview/";
    private Bitmap[] mPics = null;
    private String[] mPicPaths = null;

    private ImageView mLeftBt     = null;
    private ImageView mRightBt    = null;
    private ImageView mLeftImage  = null;
    private ImageView mRightImage = null;
    private int mCount = -1;

    private TextView mComments = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        LayoutInflater inflater = getLayoutInflater();

        ViewGroup main = (ViewGroup)inflater.inflate(R.layout.common_application_detail, null);
        setContentView(main);

        mPics = getLocalImages();

        mDownloadProgress = (TextedProgressbar)findViewById(R.id.common_app_download_progress);
        mDownloadNetwork = new NetworkImpl(this, new MyNetworkCallBack());

        Button startDownloadBt = (Button)findViewById(R.id.common_app_start_download);
        startDownloadBt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mDownloadNetwork.downloadWithPercentCallback("http://d.apk8.com:8020/CPA/newAndroidStorm_de05.apk", arg0.getContext().getFilesDir().toString());
            }
        });

        mLeftImage  = (ImageView)findViewById(R.id.common_app_detail_preview_1);
        mLeftImage.setImageBitmap(mPics[0]);
        mLeftImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent tmp = new Intent(PhoneGuideAppForMailActivity.this, ScaledImageActivity.class);
                tmp.putExtra("image", mAppRoot + mPicPath + mPicPaths[mCount]);
                startActivity(tmp);
            }

        });
        mRightImage = (ImageView)findViewById(R.id.common_app_detail_preview_2);
        mRightImage.setImageBitmap(mPics[1]);
        mRightImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent tmp = new Intent(PhoneGuideAppForMailActivity.this, ScaledImageActivity.class);
                tmp.putExtra("image", mAppRoot + mPicPath + mPicPaths[mCount + 1]);
                startActivity(tmp);
            }

        });

        mCount = 0;

        mLeftBt  = (ImageView)findViewById(R.id.common_app_detail_preview_pre);
        mLeftBt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mCount++;

                if (mCount >= mPics.length - 1) {
                    mCount--;
                    return;
                }

                mLeftImage.setImageBitmap(mPics[mCount]);
                mRightImage.setImageBitmap(mPics[mCount + 1]);
            }

        });
        mRightBt = (ImageView)findViewById(R.id.common_app_detail_preview_next);
        mRightBt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                mCount--;

                if (mCount < 0) {
                    mCount++;
                    return;
                }

                mLeftImage.setImageBitmap(mPics[mCount]);
                mRightImage.setImageBitmap(mPics[mCount + 1]);
            }

        });

        mComments = (TextView)findViewById(R.id.common_app_actionbar_comment);
        mComments.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent tmp = new Intent(PhoneGuideAppForMailActivity.this, CommentsActivity.class);
                tmp.putExtra("appid", "123");
                startActivity(tmp);
            }

        });
    }

    private Bitmap[] getLocalImages() {
        Bitmap[] ret = null;
        String dirRoot = mAppRoot + mPicPath;

        File tmp = new File(dirRoot);
        mPicPaths = tmp.list();
        ret = new Bitmap[mPicPaths.length];

        for (int i = 0; i < mPicPaths.length; i++) {
            FileInputStream fis;
            try {
                fis = new FileInputStream(dirRoot + mPicPaths[i]);
                ret[i] = BitmapFactory.decodeStream(fis);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return ret;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.phone_guide_app_for_mail, menu);
        return true;
    }

    class MyNetworkCallBack extends NetworkCallBack {
        public void onNetworkCallBack(int id, boolean success, Object attach1, Object attach2) {
            switch(id) {
            case NetworkImpl.NET_WORK_ID_DOWNLOAD_FILES:
                if (success) {
                    mDownloadProgress.setProgress(100);
                    File file = new File((String)attach2);
                    Intent intent = new Intent();
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setAction(android.content.Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                    startActivity(intent);
                } else {
                    if (mDownloadProgress != null) {
                        mDownloadProgress.setProgress((Integer)attach1);
                    }
                }
                break;
            }
        }
    }
}
